package com.eascs.ticket.configuration.controller;

import javax.inject.Inject;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.common.util.ObjectUtil;
import com.eascs.common.util.StringUtil;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.ticket.common.controller.BaseActionRestController;
import com.eascs.ticket.common.entity.EntityUtil;
import com.eascs.ticket.common.form.IdForm;
import com.eascs.ticket.common.form.OptionalIdForm;
import com.eascs.ticket.configuration.entity.ProjectCommentTemplate;
import com.eascs.ticket.configuration.entity.ProjectCommentTemplateUpdate;
import com.eascs.ticket.configuration.form.ProjectCommentTemplateForm;
import com.eascs.ticket.configuration.service.ProjectCommentTemplateService;

@RestController
public class ProjectCommentTemplateRestController extends BaseActionRestController {

	@Inject
	private ProjectCommentTemplateService projectCommentTemplateService;

	@RequestMapping("/configuration/projectCommentTemplate/editProjectCommentTemplate")
	public BaseRestResult<String> editProjectCommentTemplate(OptionalIdForm optionalIdForm) {
		if (StringUtil.hasText(optionalIdForm.getId())) {
			ViewContext.put("projectCommentTemplate", this.projectCommentTemplateService.getProjectCommentTemplateByid(optionalIdForm.getId()));
		}
		return HtmlRestResult.popHtmlFragment("/configuration/projectCommentTemplate/editProjectCommentTemplate");
	}

	@RequestMapping("/configuration/projectCommentTemplate/saveProjectCommentTemplate")
	public BaseRestResult<String> saveProjectCommentTemplate(ProjectCommentTemplateForm projectCommentTemplateForm) {
		if (StringUtil.hasText(projectCommentTemplateForm.getId())) {
			ProjectCommentTemplateUpdate projectCommentTemplateUpdate = new ProjectCommentTemplateUpdate();
			ObjectUtil.copySimpleProperties(projectCommentTemplateForm, projectCommentTemplateUpdate);
			EntityUtil.initUpdateEntity(projectCommentTemplateUpdate);
			this.projectCommentTemplateService.updateProjectCommentTemplate(projectCommentTemplateUpdate);
		} else {
			ProjectCommentTemplate projectCommentTemplate = new ProjectCommentTemplate();
			ObjectUtil.copySimpleProperties(projectCommentTemplateForm, projectCommentTemplate);
			EntityUtil.initInsertEntity(projectCommentTemplate);
			this.projectCommentTemplateService.insertProjectCommentTemplate(projectCommentTemplate);
		}
		return BaseRestResult.refresh();
	}

	@RequestMapping("/configuration/projectCommentTemplate/deleteProjectCommentTemplate")
	public BaseRestResult<String> deleteProjectCommentTemplate(IdForm idForm) {
		this.projectCommentTemplateService.deleteProjectCommentTemplateById(idForm.getId());
		return BaseRestResult.refresh();
	}

}
